//
//  SFMainTabBar.swift
//  SFWeiBo
//
//  Created by 孙凡 on 16/4/7.
//  Copyright © 2016年 Edward. All rights reserved.
//

import UIKit

class SFMainTabBar: UITabBar {

    //定义一个闭包用来回传btn点击事件
    var composeBtnClick:(() -> ())?


    override init(frame: CGRect) {

        super.init(frame: frame)

        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    private func setupUI(){

        self.addSubview(composeBtn)

        backgroundImage = UIImage(named: "tabbar_background")

    }


    override func layoutSubviews() {



        let itemW = self.frame.width / 5

        var index = 0

        super.layoutSubviews()

        for value in subviews{

            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {

                let x = CGFloat(index) * itemW

                value.frame.origin.x = x
                value.frame.size.width = itemW

                if index == 1 {

                    index += 1
                }


                index += 1
            }

        }
        composeBtn.center = CGPointMake(self.center.x, self.frame.height * 0.5)

        
    }


    private lazy var composeBtn: UIButton = {

        let button = UIButton()

        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)

        button.setImage(UIImage.init(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage.init(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)

        //添加点击事件
        button.addTarget(self, action: #selector(SFMainTabBar.composeBtnDidClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)

        button.sizeToFit()

        return button
    }()



    @objc private func composeBtnDidClick(btn:UIButton){

        composeBtnClick!()

    }


}
